<?php
class Admin_contact extends Contact_Controller{
	function __construct(){
		parent :: __construct();
		$this->load->model('Contact_m');
		$this->_data['sidebar']="contact";
		$this->load->database();
	}
	
	function index(){
		$this->list_contact();	
	}
	
	//--list contact--//
	function list_contact(){
		is_admin();
		$this->_data['page_title'] = "contact liste";
		$per_page = $this->uri->segment(3);
		$off_set = $this->uri->segment(4);
		if($per_page == ''){
			$per_page = 20;
		}
		if($off_set == ''){
			$off_set = '';
		}
		$config['base_url'] = base_url()."index.php/admin_contact/list_contact/".$per_page."/";
    	$config['per_page'] = $per_page;
    	$config['uri_segment'] = 4;
   	 	$config['total_rows'] = $this->Contact_m->count_contact();
		if ($off_set > 0 && $off_set == $config['total_rows']) {
            $off_set = $off_set - $per_page;
        }
		$result = $this->Contact_m->list_contact($per_page, $off_set);
		$this->pagination->initialize($config);
	    $this->_data['pagination'] = $this->pagination->create_links();
	    $this->_data['total'] = $config['total_rows'];
	    $this->_data['perpage'] = $per_page;
	    $this->_data['offset'] = $off_set;
		$this->_data['contacts'] = $result;
		$this->display_admin('admin/contact/list_contact');
				
	}
	
	//--add contact--//
	function add_contact(){
		is_admin();
		$this->_data['page_title'] = "Ajouter contact";
		
		$config = array(
							array("field" => "first_name", 
								  "label" => lang("first_name"),
								  "rules" => "required|xss_clean|trim"
								 ),
							array("field" => "last_name", 
								  "label" => lang("last_name"),
								  "rules" => "required|xss_clean|trim"
								 ),
							array("field" => "position", 
								  "label" => lang("position"),
								  "rules" => "required|xss_clean|trim"
								 ),
							array("field" => "email", 
								  "label" => lang("email"),
								  "rules" => "required|xss_clean|valid_email|trim|callback_check_exist_email[contact.email]"
								 )
		
				       );
		$this->form_validation->set_rules($config);
		
		if($this->input->post('add_contact')){
			if($this->form_validation->run()){
				if($this->Contact_m->add_contact()){
					$this->session->set_flashdata('error', lang('add') ." ". lang('success'));
					redirect('admin_contact/list_contact');	
				}else{
					$this->_data['add_error'] = Lang('add')." ".lang('fail')."!";
					$this->display_admin('admin/contact/add_contact');
				}		
			}else{
				$this->display_admin('admin/contact/add_contact');
			}		
		}else{
			$this->display_admin('admin/contact/add_contact');
		}
	}
	
	//--edit contact--//
	function edit_contact($contact_id = null){
		is_admin();
		$this->_data['page_title'] = lang('edit_profile')." "."contact";
		if($this->Contact_m->get_contact_details($contact_id) === FALSE){
			redirect("admin_contact");
		}
		$contact_data = $this->Contact_m->get_contact_details($contact_id);
		$this->_data['contacts'] = $contact_data;
		$this->_data['first_name'] = $contact_data->first_name;
		$this->_data['last_name'] = $contact_data->last_name;
		$this->_data['position'] = $contact_data->position;
		$this->_data['email'] = $contact_data->email;
		$this->_data['contact_id'] = $contact_data->id;	
		$config = array(
							array("field" => "first_name", 
								  "label" => lang("first_name"),
								  "rules" => "required|xss_clean|trim"
								 ),
							array("field" => "last_name", 
								  "label" => lang("last_name"),
								  "rules" => "required|xss_clean|trim"
								 ),
							array("field" => "position", 
								  "label" => lang("position"),
								  "rules" => "required|xss_clean|trim"
								 ),
							array("field" => "email", 
								  "label" => lang("email"),
								  "rules" => "required|xss_clean|valid_email|trim"
								 )
		
				       );
		$this->form_validation->set_rules($config);
		
		if($this->input->post('edit_contact')){
			if($this->form_validation->run()){
				if($this->Contact_m->edit_contact($contact_id)){
					$this->session->set_flashdata('error', lang('edit_profile') ." ". lang('success'));
					redirect('admin_contact/list_contact');	
				}	
			}else{
				$this->display_admin('admin/contact/edit_contact');
			}			
		}else{
			
			$this->display_admin('admin/contact/edit_contact');
		}
		
			
	}
	
	//--check exist email--//
	function check_exist_email($value, $str){
	$parts = explode('.', $str);
  		$query = $this->Contact_m->get_email($parts[1], $value, $parts[0]);
  		if($query){
  			if($query->num_rows()>0){
  				$this->form_validation->set_message('check_exist_email', "L'adresse e-mail existe déjà dans le système");
                return FALSE;
  			}else{
  				return TRUE;
  			}
  		}else{
  			$this->form_validation->set_message('check_exist_email', 'Query failed');
            return FALSE;
  		}
  			
  	
	}
	
	//--check exist emai edit--//
	function check_exist_email_edit($email, $id){
		$id = $contact_id;
		$count_email = $this->Contact_m->count_email($email);
		if($count_email > 0){
			$this->form_validation->set_message("check_exist_email", "L'adresse e-mail existe déjà dans le système");
			return FALSE;
		}else{
			return TRUE;
		}
	}

	//--delete contact--//
	function delete_contact($id){
		is_admin();
		$delete = $this->Contact_m->delete_contact($id);
		if($delete){
			$this->session->set_flashdata('error', lang('delete')." ".lang('success'));
			redirect('admin_contact/list_contact');
		}else{
			$this->session->set_flashdata('error', lang('delete')." ".lang('fail'));
			redirect('admin_contact/list_contact');
		}
	}

	//--delete multi contact--//
	function delete_multi_contacts(){
		is_admin();
        $list = "";
        $adelete = $this->input->post('delete');
        $N = count($adelete);
        for ($i = 0; $i < $N; $i++) {
            
            if ($adelete[$i] != "")
                $list = $list . ',' . $adelete[$i];
        }
        $list = '(' . substr($list, 1) . ')';

        $query = $this->Contact_m->delete_multi_contacts($list);
        if ($query)
            $this->session->set_flashdata('error', lang('delete') ." ". lang('success'));
        else
            $this->session->set_flashdata('error', lang('delete') ." ". lang('fail'));
        redirect('admin_contact/list_contact');
	}

	//--list site information--//
	function list_site_information(){
		is_admin();
		$this->_data['page_title'] = "Information sur le site liste";
		$result = $this->Contact_m->list_site_information();
		$this->_data['site_infor'] = $result;
		$this->display_admin('admin/contact/list_site_information');	
	}
	
	//--add site information--//
	function add_site_information(){
		is_admin();
		$this->_data['page_title'] = "Ajouter information sur le site";
		
		$config = array(
							array("field" => "website_address", 
								  "label" => "Website address",
								  "rules" => "required|xss_clean|trim"
								 ),
							array("field" => "email", 
								  "label" => lang("email"),
								  "rules" => "required|xss_clean|trim|valid_email"
								 )
							
		
				       );
		$this->form_validation->set_rules($config);
		
		if($this->input->post('add_site_infor')){
			if($this->form_validation->run()){
				$site_id = $this->Contact_m->get_site_id();
				if($site_id){
					
						if($this->Contact_m->update_site_information($site_id->id)){
							$this->session->set_flashdata('error', lang('add') ." ". lang('success'));
							redirect('admin_contact/list_site_information');	
						}else{
							$this->_data['add_error'] = Lang('add')." ".lang('fail')."!";
							$this->display_admin('admin/contact/add_site_information');
						}			
					
						
					}else{
						$id = 1;
						if($this->Contact_m->add_site_information(array("id"=>$id))){
							$this->session->set_flashdata('error', lang('add') ." ". lang('success'));
							redirect('admin_contact/list_site_information');	
						}else{
							$this->_data['add_error'] = Lang('add')." ".lang('fail')."!";
							$this->display_admin('admin/contact/add_site_information');
						}		
						
				}
				
			}else{
				$this->display_admin('admin/contact/add_site_information');
			}		
		}else{
			$this->display_admin('admin/contact/add_site_information');
		}	
	}
	
	//--edit site information--//
	function edit_site_information($id=null){
		is_admin();
		$this->_data['page_title'] = lang('edit_profile')." information sur le site";
		if($this->Contact_m->get_site_by_id($id) === FALSE){
			redirect("admin_contact/list_site_information");
		}
		$site_data = $this->Contact_m->get_site_id($id);
		$this->_data['site'] = $site_data;
		$this->_data['website_address'] = $site_data->website_address;
		$this->_data['email'] = $site_data->email;
		$this->_data['site_id'] = $site_data->id;
			
		$config = array(
							array("field" => "website_address", 
								  "label" => "Website address",
								  "rules" => "required|xss_clean|trim"
								 ),
							array("field" => "email", 
								  "label" => lang("email"),
								  "rules" => "required|xss_clean|trim|valid_email"
								 )
							
		
				       );
				       
		$this->form_validation->set_rules($config);
		
		if($this->input->post('edit_site_infor')){
			if($this->form_validation->run()){
				if($site_data){
					if($this->Contact_m->update_site_information($site_data->id)){
						$this->session->set_flashdata('error', lang('edit_profile') ." ". lang('success'));
						redirect('admin_contact/list_site_information');	
					}else{
						$this->_data['add_error'] = Lang('edit_profile')." ".lang('fail')."!";
						$this->display_admin('admin/contact/add_site_information');
					}			
				}
				
			}else{
				$this->display_admin('admin/contact/edit_site_information');
			}		
		}else{
			$this->display_admin('admin/contact/edit_site_information');
		}	
	}
	
	//--delete site information--//
	function delete_site_information($id){
		is_admin();
		$delete = $this->Contact_m->delete_site_information($id);
		if($delete){
			$this->session->set_flashdata('error', lang('delete')." ".lang('success'));
			redirect('admin_contact/list_site_information');
		}else{
			$this->session->set_flashdata('error', lang('delete')." ".lang('fail'));
			redirect('admin_contact/list_site_information');
		}
	}

	
}

	